package com.fp.m7.client.presenter;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.PlaceRequestEvent;

import com.fp.m7.client.AccountContext;
import com.fp.m7.client.place.NewCommentPlace;
import com.fp.m7.client.place.ProfilePlace;
import com.fp.m7.client.place.SettingsPlace;
import com.fp.m7.client.place.TimelinePlace;
import com.fp.m7.shared.services.AccountServiceAsync;
import com.google.inject.Inject;
import com.google.inject.Provider;

/**
 * Defines app's places management, displays a header specific for an authorized user.
 * 
 * @author 7
 */
public class AppSecuredPresenter extends AppPresenter<AppSecuredPresenter.Display> {

    public interface Display extends AppPresenter.Display {
    }

    @Inject
    public AppSecuredPresenter(Display display, EventBus eventBus, AccountContext accountContext, //
            AccountServiceAsync accountService, //
            Provider<HeaderPublicPresenter> headerPublicPresenterProvider, //
            Provider<HeaderSecuredPresenter> headerSecuredPresenterProvider, //
            Provider<LoginPresenter> loginPresenterProvider, //
            Provider<RegisterPresenter> registerPresenterProvider, //
            Provider<HomePresenter> homePresenterProvider, //
            Provider<ProfilePresenter> profilePresenterProvider, //
            Provider<NewCommentPresenter> newCommentPresenterProvider, //
            Provider<TimelinePresenter> timelinePresenterProvider, //
            Provider<SettingsPresenter> settingsPresenterProvider, //
            Provider<InfoPresenter> infoPresenterProvider) {

        super(display, eventBus, accountContext, accountService, headerPublicPresenterProvider,
                headerSecuredPresenterProvider, loginPresenterProvider, registerPresenterProvider,
                homePresenterProvider, profilePresenterProvider, newCommentPresenterProvider,
                timelinePresenterProvider, settingsPresenterProvider, infoPresenterProvider);
    }

    @Override
    public void onPlaceRequest(PlaceRequestEvent event) {
        String placeName = event.getRequest().getName();

        if (TimelinePlace.NAME.equals(placeName)) {
            display.setContent(getTimelinePresenter().getDisplay());
        } else if (NewCommentPlace.NAME.equals(placeName)) {
            display.setContent(getNewCommentPresenter().getDisplay());
        } else if (SettingsPlace.NAME.equals(placeName)) {
            display.setContent(getSettingsPresenter().getDisplay());
        } else if (ProfilePlace.NAME.equals(placeName)) {
            // in secured mode, there is default profile page for logged user (when no nickname param provided), but one
            // can view profiles of others (explicitely providing the nickname param)
            String forUser = event.getRequest().getParameter(ProfilePlace.NICKNAME, null);
            getProfilePresenter().setNickname(forUser);
            display.setContent(getProfilePresenter().getDisplay());
        } else {
            display.setContent(getTimelinePresenter().getDisplay()); // default
        }
    }

    @Override
    public boolean isForAuthenticatedUser() {
        return true;
    }

    @Override
    protected HeaderPresenter<?> getHeaderPresenter() {
        if (headerPresenter == null) {
            headerPresenter = headerSecuredPresenterProvider.get();
            headerPresenter.bind();
        }
        return headerPresenter;
    }
}
